﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_OnlineSurvey_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadAction();
            BindData();
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (tbTitle.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入您要在线调查的内容。");
            return;
        }

        if (Shove._Convert.StrToInt(tbAmount.Text.Trim(), -1) < 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入正确的投票数量。");
            return;
        }

        long ID = Shove._Convert.StrToLong(hID.Value, -1);
        DAL.Tables.tbl_Site_OnlineSurvey table = new DAL.Tables.tbl_Site_OnlineSurvey();
                    
        table.Title.Value = tbTitle.Text.Trim();
        table.Amount.Value = Shove._Convert.StrToInt(tbAmount.Text.Trim(), -1);

        bool IsShow = true;
        if(rbStop.Checked)
        {
            IsShow = false;
        }
        table.IsShow.Value = IsShow;

        if (hID.Value != "")
        {
            long i = table.Update("[ID]=" + ID);

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改成功!");
            }
        }
        else
        {
            long i = table.Insert();

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "添加成功!");

                tbTitle.Text = "";
                tbAmount.Text = "";
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OnlineSurvey_Manager.aspx");
    }

    private void ReadAction()
    {
        this.hID.Value = Shove._Web.Utility.GetRequest("ID");
    }

    private void BindData()
    {
        if (this.hID.Value != "")
        {
            long ID = Shove._Convert.StrToLong(hID.Value, -1);
            DataTable dt = null;

            try
            {
                dt = new DAL.Tables.tbl_Site_OnlineSurvey().Open("", "[ID]=" + ID, "");

                if (dt == null)
                {
                    PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);
                    return;
                }

                tbTitle.Text = dt.Rows[0]["Title"].ToString();
                tbAmount.Text = dt.Rows[0]["Amount"].ToString();
  
                bool IsShow = Shove._Convert.StrToBool(dt.Rows[0]["IsShow"].ToString(), false);
                if (IsShow)
                {
                    rbStart.Checked = true;
                    rbStop.Checked = false;
                }
                else
                {
                    rbStart.Checked = false;
                    rbStop.Checked = true;
                }
            }
            catch { }
        }
    }

}
